"""
单例设计模式：系统中只存在一个实例对象，
多次创建实例时，新实例的属性会覆盖旧实例的属性，
因为只存在一个实例对象，所以地址不变
"""


class MusicPlayer(object):
    instance = None  # 用来保存实例对象

    def __new__(cls, *args, **kwargs):
        # 1.创建对象，分配空间
        if cls.instance is None:
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self, name):
        self.name = name


if __name__ == '__main__':
    player1 = MusicPlayer('七里香')
    player2 = MusicPlayer('东风破')
    print(id(player1))
    print(id(player2))
    # 2430418720080
    # 2430418720080
    print(player1.name)
    print(player2.name)
    # 东风破
    # 东风破
